//
//  SEGTarget_Toast.m
//  Commom
//
//  Created by 山刘 on 2019/4/12.
//  Copyright © 2019年 山刘. All rights reserved.
//

#import "SEGTarget_Toast.h"

#import <Masonry/Masonry.h>

#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif

#if __has_include(<SEGPlatform/SEGPlatformManager.h>)
#import <SEGPlatform/SEGPlatformManager.h>
#else
#import "SEGPlatformManager.h"
#endif

#import "SEGNodataManager.h"
#import "UIViewController+SEGNaviAdd.h"

/// 缺省图背景tag
#define kNoDataViewTag 10000000

@implementation SEGTarget_Toast

/**
 * 显示遮罩层
 * @author Sam
 */
- (void)segAction_showCustomProgressImage:(NSDictionary *)params {
    NSMutableDictionary *dicM = [NSMutableDictionary dictionaryWithDictionary:params];
    // 顶部卡片
    UIImage *topCardImage = [UIImage imageNamed:SEGPicName(dicM[kNoDataTopCardName])];
    if (!topCardImage) topCardImage = [UIImage imageNamed:dicM[kNoDataTopCardName]];

    // 缺省图
    NSString *imageName = dicM[kNoDataImageName];
    if (!imageName) imageName = @"pic_default_page";
    UIImage *image = SEGAutoPicName(imageName);
    if (!image) image = [UIImage imageNamed:imageName];
    
    // 缺省文案
    NSString *text = dicM[kNoDataText];
    if (!text) text = kNoDataDefaultMessage;

    // 渲染
    [self showCustomProgressImage:dicM[kNoDataSuperView]
                          originY:[dicM[kNoDataOriginY] floatValue]
                     imageOriginY:[dicM[kNoDataImageOriginY] floatValue]
                          topCard:topCardImage
                        maskImage:image
                             text:text
              backgroundViewColor:dicM[kNoDataBackgroundColor]];
}

/**
 * 隐藏遮罩层
 * @author Sam
 */
- (void)segAction_hiddenCustomProgressImage:(NSDictionary *)params {
    [self hiddenCustomProgressImage:params[kNoDataSuperView]];
}

/**
 * 当加载页面没有数据时提示页
 * @author Sam
 * @param view     添加到的父视图
 * @param text     描述
 * @param originY  添加到父视图的起始y坐标
 * @param imageOriginY  缺省图图片的起始y坐标
 * @param topCardImage  缺省图顶部图片
 * @param image  缺省图
 * @param backColor 背景色
 */
- (void)showCustomProgressImage:(UIView *)view
                        originY:(CGFloat)originY
                   imageOriginY:(CGFloat)imageOriginY
                        topCard:(UIImage *)topCardImage
                      maskImage:(UIImage *)image
                           text:(NSString *)text
            backgroundViewColor:(UIColor *)backColor {
    if (!view) return;
    // 防止默认图重复显示
    [self hiddenCustomProgressImage:view];
    
    // bg
    UIView *noDataView = [[UIView alloc] init];
    noDataView.frame = CGRectMake(0, originY, view.bounds.size.width, view.bounds.size.height-originY);
    noDataView.backgroundColor = backColor ?: UIColor.clearColor;
    noDataView.tag = kNoDataViewTag;
    [view addSubview:noDataView];
    [noDataView mas_makeConstraints:^(MASConstraintMaker *make) {
        if ([view isKindOfClass:[UIScrollView class]]) {
            make.top.mas_equalTo(originY);
            make.left.right.bottom.mas_equalTo(0);
            make.height.equalTo(view.mas_height).offset(-originY);
            make.centerX.equalTo(view);
        } else {
            make.left.right.bottom.mas_equalTo(0);
            make.top.mas_equalTo(originY);
        }
    }];
    
    // 缺省图
    UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
    [noDataView addSubview:imageview];
    [imageview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(noDataView);
        if (imageOriginY > 0) {
            make.top.mas_offset(imageOriginY);
        } else {
            make.centerY.equalTo(noDataView).offset(-kNavigationBarAndStatusBarHeight/2);
        }
    }];

    // 卡片
    if (topCardImage) {
        UIImageView *topCardImgView = [[UIImageView alloc] initWithImage:topCardImage];
        [noDataView addSubview:topCardImgView];
        [topCardImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_offset(25);
            make.centerX.equalTo(noDataView);
        }];
        [imageview mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(noDataView);
            make.top.mas_offset(230);
        }];
    }
    
    // title
    CGFloat padding = 20;
    UILabel *titleLabel = [[UILabel alloc] init];
    [titleLabel setText:text];
    [titleLabel setTextAlignment:NSTextAlignmentCenter];
    [titleLabel setFont:(kFont_13)];
    [titleLabel setTextColor:kColor_3];
    [noDataView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.top.equalTo(imageview.mas_bottom).offset(padding);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
    }];
}

/**
 * 隐藏遮罩页
 * @author Sam
 * @param view  图片展示的父视图
 */
- (void)hiddenCustomProgressImage:(UIView *)view {
    for (UIView *aImageView in view.subviews) {
        if (aImageView.tag == kNoDataViewTag) {
            if ([aImageView isKindOfClass:[UIView class]]) {
                [aImageView removeFromSuperview];
            }
        }
    }
}

@end
